﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Twittervice.api
{
    /// <summary>
    /// This class represents the status update of a Twitter user
    /// </summary>
    public class TwitterStatus
    {
        public const string STATUS_TAG = "status";

        /// <summary>
        /// Default constructor
        /// </summary>
        public TwitterStatus()
        {
        }

        // The user who issues this status
        private TwitterUser user;

        public TwitterUser User
        {
            get { return user; }
            set { user = value; }
        }

        private DateTime createdAt;

        public DateTime CreatedAt
        {
            get { return createdAt; }
            set { createdAt = value; }
        }

        private long id;

        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private string source;

        public string Source
        {
            get { return source; }
            set { source = value; }
        }

        private bool truncated;

        public bool Truncated
        {
            get { return truncated; }
            set { truncated = value; }
        }

        private long inReplyToStatusId;

        public long InReplyToStatusId
        {
            get { return inReplyToStatusId; }
            set { inReplyToStatusId = value; }
        }

        private long inReplyToUserId;

        public long InReplyToUserId
        {
            get { return inReplyToUserId; }
            set { inReplyToUserId = value; }
        }

        private bool favorited;

        public bool Favorited
        {
            get { return favorited; }
            set { favorited = value; }
        }

        private string inReplyToScreenName;

        public string InReplyToScreenName
        {
            get { return inReplyToScreenName; }
            set { inReplyToScreenName = value; }
        }

        /// <summary>
        /// Parses an xml structure to create a TwitterStatus object.
        /// </summary>
        /// <param name="nodeList">XML nodes</param>
        /// <returns>The associcated TwitterStatus object</returns>
        public static TwitterStatus parseXml(XmlNodeList nodeList)
        {
            TwitterStatus status = new TwitterStatus();

            foreach (XmlNode n in nodeList)
            {
                switch (n.Name)
                {
                    case "created_at":
                        status.createdAt = DateTime.ParseExact(n.InnerText,
                            "ddd MMM dd HH:mm:ss zzz yyyy",
                            System.Globalization.CultureInfo.InvariantCulture);
                        break;

                    case "id":
                        status.Id = long.Parse(n.InnerText);
                        break;

                    case "text":
                        status.Text = n.InnerText;
                        break;

                    case "source":
                        status.Source = n.InnerText;
                        break;

                    case "truncated":
                        status.Truncated = bool.Parse(n.InnerText);
                        break;

                    case "in_reply_to_status_id":
                        if (n.InnerText != "")
                        {
                            status.InReplyToStatusId = long.Parse(n.InnerText);
                        }
                        break;

                    case "in_reply_to_user_id":
                        if (n.InnerText != "")
                        {
                            status.InReplyToUserId = long.Parse(n.InnerText);
                        }
                        break;

                    case "favorited":
                        status.Favorited = bool.Parse(n.InnerText);
                        break;

                    case "in_reply_to_screen_name":
                        if (n.InnerText != "")
                        {
                            status.inReplyToScreenName = n.InnerText;
                        }
                        break;

                    case "user":
                        status.user = TwitterUser.parseXml(n.ChildNodes);
                        break;
                }
            }
            return status;
        }
    }
}
